require([
  'jquery',
  'pageInit',
  'dataTableFilter',
  'loading',
  'notification'
], function($, vamakerPage, VamDataTable, VamLoader, VamOpMsg) {
  $(function() {
    var 
      $dataTable = $('#data-table'),
      dataUrl = VAM.getVar('LAUNCH_DATA_LIST'),
      dataEidtUrl = VAM.getVar('DATA_EDIT_URL'),
      dataLinkUrl = VAM.getVar('LINK_URL'),
      isAllBrand = VAM.getVar('IS_ALL_BRAND'),
      filterStatus = VAM.getVar('FILTER_STATUS'),
      $reportDataThead = $dataTable.find('thead'),
      $loadingWrap = new VamLoader().loader($('#list-loading-wrap')),
    listTable, tableOptions = {};
    $('.new-operate-btn').on('click', function(evt){
      if (isAllBrand) {
        evt.preventDefault();
        $('#all_tip').removeClass('hidden');
      }
    });

    TabAsyncOp = function (type, data, $btn, list) {
      console.log(type);
      if (type === 'delete') {
        $.ajax({
          url: VAM.getVar('LIST_DELETE_OP_URL'),
          type: 'GET',
          dataType: 'JSON',
          data: data
        })
        .done(function(data) {
          if (data.status === 'success') {
            VamOpMsg.messager(data);
            list.ajax.reload();
          } else {
            VamOpMsg.messager(data);
          };
        })
        .fail(function() {
          console.log("error");
        })
        .always(function() {
          $btn.button('reset');
        });     
      } else if (type === 'pause') {
        $.ajax({
          url: VAM.getVar('LIST_PAUSE_OP_URL'),
          type: 'GET',
          dataType: 'JSON',
          data: data
        })
        .done(function(data) {
          if (data.status === 'success') {
            VamOpMsg.messager(data);
            list.ajax.reload();
          } else {
            VamOpMsg.messager(data);
          };
        })
        .fail(function() {
          console.log("error");
        })
        .always(function() {
          $btn.button('reset');
        });     
      } else if (type === 'resume') {
        $.ajax({
          url: VAM.getVar('LIST_RESUME_OP_URL'),
          type: 'GET',
          dataType: 'JSON',
          data: data
        })
        .done(function(data) {
          if (data.status === 'success') {
            VamOpMsg.messager(data);
            list.ajax.reload();
          } else {
            VamOpMsg.messager(data);
          };
        })
        .fail(function() {
          console.log("error");
        })
        .always(function() {
          $btn.button('reset');
        });     
      };
    };
    // Table settings.
    tableOptions = {
      'ajax': {
        'url': dataUrl
      },
      'order': [11, 'desc'],
      'columns': [
        {'data': 'id'},
        {'data': 'name'},
        {'data': 'campaign'},
        {'data': 'status'},
        // {'data': 'frequency_control'},
        {'data': 'launchTime'},
        {'data': 'strategyNum'},
        {'data': 'cost'},
        {'data': 'showCount'},
        {'data': 'clickCount'},
        {'data': 'clickRate'},
        // {'data': 'cpm'},
        {'data': 'cpc'},
        {'data': 'modifiedDate'},
        {'data': 'operate'}
      ],
      'columnDefs': [
        {
          'targets': 0,
          'render': function (data, type, full, meta) {
            var ckbText = '<label><input class="ckb-children" type="checkbox" value="' + data + '"/></label>';
            ckbText = '';
            return '<p title="' + data + '">' + ckbText + data +'</p>';
          }
        },
        {
          'targets': 3,
          'render': function (data, type, full, meta) {
            var result = '';
            if (data === 'archived') {
              result = VAM.j('Archived');
            } else if (data === 'effective') {
              result = VAM.j('Effective');
            } else if (data === 'incomplete') {
              result = VAM.j('Incomplete');
            } else if (data === 'waiting') {
              result = VAM.j('Waiting');
            } else if (data === 'paused') {
              result = VAM.j('Paused');
            } else if (data === 'closed') {
              result = VAM.j('Closed');
            };
            return '<p title="' + result + '">' + result +'</p>';
          }
        },
        {
          'targets': [2,4,7,8,11],
          'render': function (data, type, full, meta) {
            return '<p title="' + data + '">' + data +'</p>';
          }
        },
        {
          'targets': [7,9],
          'visible': filterStatus,
        },
        {
          'targets': [1,5],
          'render': function (data, type, full, meta) {
            var result;
            if (full.strategyNum === 0) {
              return '<p title="' + data + '">' + data +'</p>';
            };
            result = '<p title="' + VAM.j('Click to view the launch corresponding strategy') + '"><a href="' + dataLinkUrl + '?campaign_id=' + full.id + '" target="_blank">' + data + '</a></p>';
            return result;
          }
        },
        {
          'targets': [6,10],
          'render': function (data, type, full, meta) {
            return '<p class="text-success" title="' + data + '"><i class="fa fa-rmb"></i> ' + data + '</p>';
          }
        },
        {
          'targets': 9,
          'render': function (data, type, full, meta) {
            return '<p class="text-default" title="' + data + '">' + data + ' %</p>';
          }
        },
        {
          'targets': -1,
          'render': function (data, type, full, meta) {
            var pausedStopStr = '';
            if (full.status === 'effective') {
              // launch could pause.
              pausedStopStr = '<button class="btn btn-danger btn-xs tab-pasue" data-type="pause" data-loading-text="<i class=\'fa fa-spinner fa-spin\'></i>" title="' + VAM.j('Pause') + '"><i class="fa fa-pause"></i></button>';
            } else if (full.status === 'paused') {
              // launch could resume.
              pausedStopStr = '<button class="btn btn-success btn-xs tab-resume" data-type="resume" data-loading-text="<i class=\'fa fa-spinner fa-spin\'></i>" title="' + VAM.j('Start') + '"><i class="fa fa-play-circle-o"></i></button>';
            };
            return '<div class="tab-btn-group">'
                  + '<input type="hidden" class="row-info" value="' + full.id + '"/>'
                  + '<a href="' + dataEidtUrl + '?id=' + full.id + '" class="btn btn-primary btn-xs tab-edit" data-type="edit" data-loading-text="<i class=\'fa fa-spinner fa-spin\'></i>" title="' + VAM.j('Edit') + '"><i class="fa fa-edit"></i></a>'
                  // + pausedStopStr
                  // + '<a href="#" class="btn btn-info btn-xs tab-report" data-type="report" data-loading-text="<i class=\'fa fa-spinner fa-spin\'></i>" title="报表"><i class="fa fa-file-text"></i></a>'
                  // + '<button class="btn btn-success btn-xs tab-more" data-type="view" title="查看"><i class="fa fa-eye"></i></button>'
                  + '</div>';
          }
        }
      ],
      'vamSettings': {
        'vamColumnFilter': false,
      },
      'initComplete': function() {
        tableResize = new VamDataTable().columnResize($dataTable);
      },
    };
    $dataTable
    .on('preXhr.dt', function(evt) {
      $loadingWrap.show();
    });
    listTable = new VamDataTable().dataTables($dataTable, tableOptions);
    // After table data list.
    listTable.on('xhr.dt', function(evt) {
      $loadingWrap.fadeOut();
    });

    $dataTable.on('click', 'tbody td .tab-pasue', function (evt) {
      var $me = $(this),
        $rowInfo = $me.siblings('.row-info'),
        opId = $rowInfo.val(),
        opType = $me.data('type'),
        opData;
      evt.preventDefault();
      opData = {
        solution_id: opId,
        type: opType,
        rnd: (new Date()).getTime()
      };
      $me.button('loading');
      TabAsyncOp(opType,opData, $me, dataList);
    });

    $dataTable.on('click', 'tbody td .tab-resume', function (evt) {
      var $me = $(this),
        $rowInfo = $me.siblings('.row-info'),
        opId = $rowInfo.val(),
        opType = $me.data('type'),
        opData;
      evt.preventDefault();
      opData = {
        solution_id: opId,
        type: opType,
        rnd: (new Date()).getTime()
      };
      $me.button('loading');
      TabAsyncOp(opType,opData, $me, dataList);
    });
  });
});
